<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>
    scheduled tasks don&#39;t complete : Forums : PythonAnywhere
</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Forums : PythonAnywhere">
        <meta name="author" content="PythonAnywhere LLP">
        <meta name="google-site-verification" content="O4UxDrfcHjC44jybs2vajc1GgRkTKCTRgVzeV6I9V14" />

        <!-- Le styles -->
        <link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet">
        <link href="/static/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="/static/anywhere/styles/bootstrap_base.css" rel="stylesheet">
        
    
    <link rel="stylesheet" href="/static/anywhere/styles/forums.css" type="text/css" media="screen" charset="utf-8" />

    <link rel="stylesheet" href="/static/pygments/style.css" type="text/css" media="screen" charset="utf-8" />

        <link rel="stylesheet" href="/static/jquery/jquery-ui-1.8.11.custom.css" type="text/css" media="screen" charset="utf-8" />
        
    
    <link rel="alternate" type="application/rss+xml" title="RSS" href="/forums/topic/744/rss" />

        <style type="text/css">
            body {
                height: auto;
            }
        </style>
        <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
        <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <!-- Le fav and touch icons -->
        <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
        <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
        <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
    </head>

     <body>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div id="id_internal_nav_bar_container" class="container">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="/"><img id="id_logo" src="/static/anywhere/images/logo-234x35.png" height="35" title="PythonAnywhere logo" alt="PythonAnywhere logo" /></a>
                    <div class="nav-collapse">
                        <ul id="id_header_links" class="nav">
                            <li><a id="id_feedback_link" class='feedback_link' href="">Send feedback</a></li>
                            <li><a id="id_forums_link" href="/forums/">Forums</a></li>
                            <li><a href="/wiki/" id="id_help_link">Help</a></li>
                            <li><a href="http://blog.pythonanywhere.com/" id="id_blog_link">Blog</a></li>
                            
                            
                                <li><a style="font-weight: bold;" href="/pricing/" id="id_pricing_link">Pricing & signup</a></li>
                                <li><a href="/login/?next=/forums/topic/744/" id="id_login_link">Log in</a></li>
                            
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="top-container" id="id_dummy"></div>

        

        
    
    <div class="container">
        <div class="row">
            <div class="span8 offset3">
                <h1>PythonAnywhere Forums</h1>
            </div>
        </div>



    <div class="row">
        <div class="span8 offset3">
            <p>
                <a href="/forums/start_topic/" id="id_start_new_topic">Start a new topic</a> |
                <a href="/forums/">Back to all topics</a> |
                <a href="/forums/topic/744/rss">This topic's RSS</a>
            </p>
        </div>
    </div>
    <div class="row">
        <div class="span8 offset3 topic">
            <h2>scheduled tasks don&#39;t complete</h2>
        </div>
    </div>

    
        <div class="row">
            <div id="id_post_5322" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5322_content_text" class="span8">
                        <p>I've scheduled an hourly task. The task runs every hour but doesn't seem to complete - "Currently Running Tasks" shows several of them now (one per hour), waiting to be killed. My app is written in web2py. The task is a bash script which calls two web2py/python scripts.</p>
<p>The python scripts don't ask for input. If I run the bash script from the console, it works perfectly and exits.</p>
<p>Any ideas? thanks</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5322_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5322_created_date">
                            July 5, 2013, 12:04 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5322">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5327" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5327_content_text" class="span8">
                        <p>Can you add some debugging output to the task?  maybe a few print statements, so that you can see how far it gets before it gets stuck?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5327_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            harry
                            |
                            376
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5327_created_date">
                            July 5, 2013, 2:21 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5327">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5328" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5328_content_text" class="span8">
                        <p>It uses the Python logging module (import logging) to print its messages, but it's not printing anything... let me see if I can put a print at the very beginning. From the interactive bash console, it works flawlessly.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5328_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5328_created_date">
                            July 5, 2013, 2:32 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5328">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5329" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5329_content_text" class="span8">
                        <p>No love. The scripts don't print anything. The jobs seem to be stuck at the very beginning, waiting to be killed.</p>
<p>Would you mind taking a look at my bin/refresh_cache.sh script? It works fine and all debug messages are printed when running from console.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5329_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5329_created_date">
                            July 5, 2013, 3:24 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5329">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5331" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5331_content_text" class="span8">
                        <p>I have also had this problem before. And I still can't figure out what's happening (no log prints, no ressource consumption). Any explanations?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5331_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            radotranonkala
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5331_created_date">
                            July 5, 2013, 4:12 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5331">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5332" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5332_content_text" class="span8">
                        <p>I found several instances running on the server.  The problem is that the logs only get written when the process exits, and it looks like your script is hanging / never finishes.</p>
<p>You can use the "kill" link next to a running task to stop them, and then see what output they printed.  Does that help you to figure out why they are hanging?</p>
<p>Another thing you could try is to setup the logging module to write to a file, eg in /tmp.  That should happen immediately, so you will be able to see anything you write to it in real-time, either from a bash console or via the files tab..</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5332_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            harry
                            |
                            376
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5332_created_date">
                            July 5, 2013, 5:17 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5332">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5334" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5334_content_text" class="span8">
                        <p>Now everything seems to be working. Not sure why, but it seems like the processes finish now. I'll report back after the next couple of hourly runs.</p>
<p>I found an "echo hello world" job that I didn't submit; perhaps one of the devs tested something out on my account? Any ideas on what could have changed? thanks for the assist.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5334_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5334_created_date">
                            July 5, 2013, 7:47 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5334">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5336" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5336_content_text" class="span8">
                        <p>So, reporting back. The scheduled tasks are now running just fine. I didn't change anything, so I assume it was something server-side. Thanks!</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5336_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5336_created_date">
                            July 6, 2013, 12:01 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5336">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5344" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5344_content_text" class="span8">
                        <p>@rcardenas -- thanks for confirming that they're working now.  Harry and I will take a further look on Monday to see if we can pin this down a bit better. </p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5344_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            giles
                            |
                            866
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5344_created_date">
                            July 7, 2013, 5:53 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5344">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5351" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5351_content_text" class="span8">
                        <p>Right, we can't see any problems on the server at the time you reported this. Is it possible that your script was just not exiting?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5351_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            giles
                            |
                            866
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5351_created_date">
                            July 8, 2013, 11:48 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5351">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5409" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5409_content_text" class="span8">
                        <p>Thanks for your followup Giles. I don't think so; the script shouldn't take hours to finish.</p>
<p>It just started happening again today. Now I'm getting this in the log:</p>
<p>refresh_cache.sh: started
/home/rcardenas/bin/refresh_cache.sh: fork: retry: No child processes
/home/rcardenas/bin/refresh_cache.sh: fork: retry: No child processes
/home/rcardenas/bin/refresh_cache.sh: fork: retry: No child processes
/home/rcardenas/bin/refresh_cache.sh: fork: retry: No child processes
/home/rcardenas/bin/refresh_cache.sh: fork: Resource temporarily unavailable</p>
<p>Could you take a look? thanks, best regards -Ricardo</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5409_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5409_created_date">
                            July 15, 2013, 1:27 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5409">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5411" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5411_content_text" class="span8">
                        <p>If your script is short and doesn't contain anything private you could post it here for people to take a look at, in case there's some subtle problem we can spot. Make sure it doesn't contain any hard-coded passwords or similar, of course, and if you do post it, please indent each line by 4 spaces to make sure it's formatted nicely (and be careful not to change the indentation of the lines relative to each other in the process).</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5411_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            Cartroo
                            |
                            638
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5411_created_date">
                            July 15, 2013, 2:08 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5411">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5413" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5413_content_text" class="span8">
                        <p>"Resource temporarily unavailable" is the error you get when you try and start too many processes.  We have a ulimit of 32 processes per user on the task server, to prevent fork bombs...</p>
<p>Could your script be stuck in some kind of loop, trying to spawn more and more processes?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5413_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            harry
                            |
                            376
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5413_created_date">
                            July 16, 2013, 11:04 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5413">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5727" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5727_content_text" class="span8">
                        <p>@harry @cartroo - Just came back from 15 days' vacation with no computer access. Thanks for your comments in the meantime.</p>
<p>The python (web2py) script doesn't spawn processes. It executes a few queries against MySQL, iteratively, and iterates over the results. I've now put debugging comments into my script, and after testing the job for a few cycles, I've identified that the python script is stopped after issuing a query to the MySQL database (not even a second since starting the script). It is not the first query in the script - in fact it is the same query that has run several times already, with different values. The script gets stuck at the query and never completes.</p>
<p>I suppose I could add logic so that the script terminates when it encounters this condition. However, I do need all the queries to run.</p>
<p>The strange thing is that the script runs perfectly fine, every time, when run from the command line (in an ssh terminal).</p>
<p>Does that help narrow it down?</p>
<p>ps. There are no forks, nor too many processes, etc. It's a simple linear script with queries and normal logic.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5727_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5727_created_date">
                            July 31, 2013, 3:21 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5727">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5728" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5728_content_text" class="span8">
                        <p>Hmm, OK.  I reckon that the "Resource temporarily unavailable" error was happening because the processes were piling up due to it not exiting, and reached the limit of 32.  But that still doesn't explain <em>why</em> they weren't exiting.  </p>
<p>I see you gave us permission to take a look at your script back at the start of this thread, so I'll see if there's anything I can see there.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5728_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            giles
                            |
                            866
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5728_created_date">
                            July 31, 2013, 3:29 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5728">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5731" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5731_content_text" class="span8">
                        <p>Definitely odd.  I've confirmed that although your scheduled task appears to be running, there are no queries running as you on the MySQL server associated with your account.  </p>
<p>There are two copies of your scheduled task running, though.  And that in itself is a little odd; one was started at 14:59 and one at 15:13; I see you have scheduled tasks set up to start at those times.  But I can also see that only the latter is shown in your "Currently running tasks" list.</p>
<p>Is it possible that the first one (started at 14:59) was still running without blocking, and then the other one kicked off, and because they were both running at the same time they got into some kind of deadlock?</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5731_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            giles
                            |
                            866
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5731_created_date">
                            July 31, 2013, 3:41 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5731">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5734" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5734_content_text" class="span8">
                        <p>@giles Thanks for your analysis.</p>
<p>I doubt the problem is db deadlocks. (In fact, only today did I separate the tasks into the two hourly tasks to aid debugging. Previously it all ran linearly off of one shell script, and exhibited the same behavior.)</p>
<p>Thanks for your offer to check the scripts. Let me explain briefly:</p>
<p>(1) refresh_cache.sh runs hourly and logs to /tmp/refresh_cache.log. (It actually contains some logic to not do anything between 01:00am and 07:00am my local time, nor on weekends.)</p>
<p>(1.1) refresh_cache.sh runs a web2py script called refresh_cache.py which logs to /tmp/seaceperu.log. It does some screen scraping and upserts to MySQL.</p>
<p>(1.2) Then, refresh_cache.sh runs a web2py script called send_change_alerts.py, which queries the database for new content, alerts the users via email, and updates a few records in MySQL.</p>
<p>Step (1) as a whole takes under a minute, sometimes up to two.</p>
<p>(2) The web2py script new_prospects.py also runs hourly. It logs to /tmp/new_prospects.log. (It should be running off of refresh_cache.sh, but I took it out for debugging purposes.) This script queries the database, may send some emails, and may do some MySQL updates. It seems to stall at different places each time, which appears to rule out some problem with the queries or the data. This script also normally completes within a minute.</p>
<p>Steps (1) and (2) always work fine when run off the command line. But when run from the Schedule tab, the jobs can get stuck at different places; if left unattended, they do indeed start piling up.</p>
<p>(3) There is a daily web2py script called send_nearby_milestones.py which queries the database and sends some emails. I haven't yet seen it give any trouble.</p>
<p>Please feel free to review the code or check the mysql logs. I'll keep debugging to see if it's a problem in the code, but right now it seems to me that something is amiss with the MySQL connection. I doubt deadlocks are the issue because this is all linear. Thanks for your assistance!</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5734_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5734_created_date">
                            July 31, 2013, 10:42 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5734">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5740" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5740_content_text" class="span8">
                        <p>@PA @giles - I've kept debugging and getting strange results. The scripts always complete perfectly when run from the command line. But when I run them from Schedule, they rarely complete.</p>
<p>I've continued adding logging and debugging to my scripts, and I've found that the scripts stop at different and unexpected places, but not far off from each other (sorry, difficult to explain :).. Sometimes the scripts aren't even doing database work when they stop. For example, just recently a script stopped between two log statements separated by an if! like so (pseudocode):</p>
<div class="codehilite"><pre><span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Print #1&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">:</span>
    <span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Print #2&quot;</span><span class="p">)</span>
<span class="nl">else:</span>
    <span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Print #3&quot;</span><span class="p">)</span>
</pre></div>


<p>The log only shows up to "Print #1" - it does not print #2 or #3. Very strange! How could this be? Is there some kind of CPU limiter or other limiter imposed on Scheduled tasks? My scripts only run for about a minute or two. And I'm always way under my 20,000 CPU-seconds limit.</p>
<p>Is there a more direct channel I might use to interact with a Support rep or Dev? I can't rule out a bug in my scripts, but the behavior I'm seeing is very strange. To emphasize, all scripts work perfectly when run from the ssh command line. thanks -Ricardo</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5740_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5740_created_date">
                            Aug. 1, 2013, 3:17 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5740">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5743" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5743_content_text" class="span8">
                        <p>@hansel @giles - I've continued debugging and uncovered something even stranger. On a lark, I decided to add consecutive logging statements inside my main loop, like so:</p>
<div class="codehilite"><pre><span class="n">some</span> <span class="n">code</span>
<span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;logging again&quot;</span><span class="p">)</span>
<span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;logging again&quot;</span><span class="p">)</span>
<span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;logging again&quot;</span><span class="p">)</span>
<span class="n">log</span><span class="p">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;logging again&quot;</span><span class="p">)</span>
<span class="n">some</span> <span class="n">more</span> <span class="n">code</span>
</pre></div>


<p>My script is still hanging at random places -- but, believe it or not, once it hung in the middle of these logging statements. i.e. the log only shows the first two of these log statements, and then nothing more.</p>
<p>So now I'm at a real loss for explanations. This doesn't seem to be a problem with my code. It seems to be some limitation imposed by (or on) the server that runs the Scheduled Tasks?</p>
<p>@hansel Thanks for the g+ hangout earlier. I added exception debugging, but no exceptions have been logged.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5743_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5743_created_date">
                            Aug. 1, 2013, 8:08 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5743">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5746" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5746_content_text" class="span8">
                        <p>I believe we have a suspect: There is a bug in our scheduled task runner that means that if the process produces too much output, a buffer gets full and the whole thing locks up. Try adding <code>&gt; /tmp/scheduled_task_output 2&gt;&amp;1</code> to the end of your scheduled task command. You should then see that  <code>/tmp/scheduled_task_output</code> contains the output of your process and your task should complete.</p>
<p>Let me know how that goes.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5746_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            glenn
                            |
                            418
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5746_created_date">
                            Aug. 2, 2013, 10:45 a.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5746">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5753" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5753_content_text" class="span8">
                        <p>@glenn @PA</p>
<p>Bingo! I think that bug might be the culprit. I have been testing your suggestion for a few hours and it has been working so far. Will keep you updated in the next few days.</p>
<p>For anyone else reading this thread: Adding @glenn 's suggestion to the actual scheduled task command in the web interface didn't quite work for me. (Either the task runner or I are mangling the extra arguments.) However, adding @glenn 's suggestion to my scripts themselves --thus ensuring that they produce very little or no output to stdout/stderr-- indeed seems to have worked around this bug.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5753_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5753_created_date">
                            Aug. 3, 2013, 2:28 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5753">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5760" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5760_content_text" class="span8">
                        <p>Excellent news, thanks for confirming!  As I said over email, Glenn has been putting together a fix for the bug and if all looks good over the next few days we'll look at pushing that one through quickly.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5760_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            giles
                            |
                            866
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5760_created_date">
                            Aug. 3, 2013, 7:02 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5760">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5766" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5766_content_text" class="span8">
                        <p>Handling output on multiple file descriptors safely in a way which can't deadlock is surprisingly subtle. I wrote a <a href="http://www.andy-pearce.com/wiki/notes/python_process_poller">Python class for doing this with <code>subprocess</code></a> awhile ago, in case anyone finds it useful (it's mostly proof-of-concept code, so beware the odd bug or two).</p>
<p>If stdout and stderr output continue to be a problem after these changes, it is possible to manually close those filehandles - this is typically done in daemon processes when forking away from the controlling terminal. A simple example of closing stdout:</p>
<div class="codehilite"><pre><span class="o">&gt;&gt;&gt;</span> <span class="kn">import</span> <span class="nn">os</span>
<span class="o">&gt;&gt;&gt;</span> <span class="kn">import</span> <span class="nn">sys</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">print</span> <span class="s">&quot;hello&quot;</span>
<span class="n">hello</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;hello&quot;</span>
<span class="n">hello</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">fileno</span><span class="p">())</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">print</span> <span class="s">&quot;hello&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;hello&quot;</span>
<span class="n">hello</span>
</pre></div>


<p>(You should be careful of closing both stdout and stderr in the interactive terminal because then you will lose your prompt and any output).</p>
<p>Of course, fixing the issues is preferable, but this might be an easy way for people to work around the problem at the start of their script until a fix is out there. The nice thing is that since you're closing the descriptor at the OS level, there's no way some errant library or hard-coded piece of Python can somehow still produce output. The disadvantage, however, is that since you're closing the descriptor at the OS level, there's no way some errant library or hard-coded piece of Python can somehow still produce output - this can make tracking down problems tricky in some cases, even with comprehensive logging.</p>
<p>By the way, sorry for not jumping in earlier - the combination of a week off between jobs and our home PC having blown up isn't good for my forum activity... Writing this length of message on my iPhone isn't my idea of fun, even with a bluetooth keyboard! (^_^)</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5766_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            Cartroo
                            |
                            638
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5766_created_date">
                            Aug. 3, 2013, 9:51 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5766">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5820" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5820_content_text" class="span8">
                        <p>@cartroo - thanks for the enlightening conversation on taking more control over stdout/stderr output.</p>
<p>@giles, @glenn - confirming that my scripts have now been running with the workaround, without a hitch for the few days - it's been around 100 runs. We can safely assume that the scheduled task runner bug @glenn suspected was the culprit.</p>
<p>Thanks for tracking the problem down. Have you pushed out the fix live?</p>
<p>Ricardo</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5820_creator_details">
                            
                                
                                    <img src="/static/anywhere/images/star.gif" alt="Beloved premium user" title="Beloved premium user"/>
                                
                            
                            rcardenas
                            |
                            14
                            posts
                            |
                            
                        </span>
                        <span id="id_post_5820_created_date">
                            Aug. 8, 2013, 2:51 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5820">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    
        <div class="row">
            <div id="id_post_5837" class="span8 offset3 post">
                <div class="row">
                    <div id="id_post_5837_content_text" class="span8">
                        <p>no, it's in the pipeline though.  will keep you posted.</p>
                    </div>
                </div>
                <div class="row">
                    <div class="span7 post_metadata">
                        <span id="id_post_5837_creator_details">
                            
                                <img src="/static/anywhere/images/staff.png" alt="Staff" title="Staff"/>
                            
                            harry
                            |
                            376
                            posts
                            |
                            
                                PythonAnywhere staff
                                |
                            
                        </span>
                        <span id="id_post_5837_created_date">
                            Aug. 9, 2013, 3:54 p.m.
                        </span>
                        |
                        <a href="https://www.pythonanywhere.com/forums/topic/744/#id_post_5837">permalink</a>
                    </div>
                    <div class="span1">
                        
                    </div>
                </div>
            </div>
        </div>
    

    




        <div id="id_footer" class="container">

            <footer id="id_copyright_div">
                <p>
                    Copyright &copy; 2013 PythonAnywhere LLP
                    &mdash;
                    <a href="/terms/">Terms</a>
                    &mdash;
                    <a href="/privacy/">Privacy</a><br/>
                    "Python" is a registered trademark of the Python Software Foundation.
                </p>
            </footer>

        </div>

        
        

        <div id="id_feedback_dialog" title="Help us improve" style="display:none">
    <div id="id_feedback_dialog_blurb_big" class="dialog_blurb_big">
        It's always a pleasure to hear from you!
    </div>
    <div id="id_feedback_dialog_blurb_small">
        Ask us a question, or tell us what you love or hate about PythonAnywhere.<br/>
        We'll get back to you over email ASAP.
    </div>
    <textarea id="id_feedback_dialog_text" rows="6"></textarea>
    <input id="id_feedback_dialog_email_address" type="text" class="default_prompt"/>
    <div id="id_feedback_dialog_error" class="pa_hidden">
        Sorry, there was an error connecting to the server. <br/>Please try again in a few moments...
    </div>
    <div class="dialog_buttons">
        <img id="id_feedback_dialog_spinner" src="/static/anywhere/images/spinner-small.gif" />
        <button class="btn btn-primary" id="id_feedback_dialog_ok_button">OK</button>
        <button class="btn" id="id_feedback_dialog_cancel_button">Cancel</button>
    </div>
</div>


        <!-- Le javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script type="text/javascript" src="/static/jquery/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="/static/jquery/jquery-ui-1.8.11.custom.min.js"></script>
        <script src="/static/bootstrap/js/bootstrap-transition.js"></script>
        <script src="/static/bootstrap/js/bootstrap-alert.js"></script>
        <script src="/static/bootstrap/js/bootstrap-modal.js"></script>
        <script src="/static/bootstrap/js/bootstrap-dropdown.js"></script>
        <script src="/static/bootstrap/js/bootstrap-scrollspy.js"></script>
        <script src="/static/bootstrap/js/bootstrap-tab.js"></script>
        <script src="/static/bootstrap/js/bootstrap-tooltip.js"></script>
        <script src="/static/bootstrap/js/bootstrap-popover.js"></script>
        <script src="/static/bootstrap/js/bootstrap-button.js"></script>
        <script src="/static/bootstrap/js/bootstrap-collapse.js"></script>
        <script src="/static/bootstrap/js/bootstrap-carousel.js"></script>
        <script src="/static/bootstrap/js/bootstrap-typeahead.js"></script>

        <script type="text/javascript">
            var urls = {};
            var Anywhere = {};
            Anywhere.urls = {};
        </script>
        <script type="text/javascript" src="/static/anywhere/scripts/ajax_utils.js"></script>

        <script type="text/javascript" src="/static/anywhere/scripts/feedback_dialog.js"></script>
        
        <script type="text/javascript">
            $(function() {
                $.extend(
                    urls,
                    {
                        feedback: '/feedback/submit/'
                    }
                );
                
                    Anywhere.FeedbackDialog.Initialise(urls, false);
                
            });
        </script>

        <script type="text/javascript">

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-18014859-6']);
            _gaq.push(['_trackPageview']);

            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();

        </script>

        



    </body>
</html>
